package br.uefs.dexa.rfframework.config;



import java.util.List;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.BasicBuilderProperties;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;




public class AppConfigManager {
	
	private static final String LIST_DELIMITER = ",";
	
	private static final String CONFIG_FILE_PATH = "resources/config/appconfig.properties";
	
	private static AppConfigManager instance;
	
	private Configuration config;
	
	private FileBasedConfigurationBuilder builder;

	
	private AppConfigManager()
	{
		initialize();
		//System.out.println("lendo do arquivo properties: " + config.getString("TEST"));
		//System.out.println("lendo do arquivo properties: " + config.getString("TESTE", System.getProperty("user.home")));
	}
	
	
	private void initialize()
	{
		Parameters params = new Parameters();
		builder = new FileBasedConfigurationBuilder(PropertiesConfiguration.class)
	    .configure(params.properties().setFileName(CONFIG_FILE_PATH));
		try
		{
		    this.config = builder.getConfiguration();
		}
		catch(ConfigurationException cex)
		{
		    cex.printStackTrace();
		}
	}
	
	public static AppConfigManager getInstance()
	{
		if(instance == null)
			instance = new AppConfigManager();
		
		return instance;
	}
	
	// ------- file system paths --------//
	
	public String getImageMapPath()
	{
		return config.getString("IMAGE_MAP_PATH");
	}
	
	public String getAnnotationMapPath()
	{
		return config.getString("ANOTTATION_MAP_PATH");
	}
	
	public String getImageDirectoryPath()
	{
		return config.getString("IMAGE_DIR_PATH");
	}
	
	public String getAnnotationDirectoryPath()
	{
		return config.getString("ANOTTATION_DIR_PATH");
	}
	
	public String getLireIndexFile()
	{
		return config.getString("LIRE_INDEX_FILE_PATH");
	}
	
	
	// ------- features --------//
	
	public String[] getFeatures()
	{
		return config.getString("FEATURES").split(LIST_DELIMITER);
	}
	
	
	
	// ------- paging settings --------//
	
	public int getNumberOfRetrievedObjectsShown()
	{
		return config.getInt("NUMBER_OF_RETRIEVED_OBJECTS_SHOWN");
	}
	
	public int getMaxItensPerPage()
	{
		return config.getInt("MAX_ITENS_PER_PAGE");
	}
	
	public int getProgressBarTreshhold()
	{
		return config.getInt("PROGRESS_BAR_TRESHOLD");
	}
	
//	public void testAdd()
//	{
//		config.addProperty("colors.pie",
//				  new String[] { "#FF0000", "#00FF00", "#0000FF" });
//		
//		try {
//			builder.save();
//		} catch (ConfigurationException e) {
//			e.printStackTrace();
//		}
//		
//	}
	
//	public String testInclude()
//	{
//		return config.getString("NAME");
//	}
	
	public List<Object> testInclude()
	{
		return config.getList("NAME");
	}
	
	public void testSave() throws ConfigurationException
	{
		this.config.setProperty("TEST_KEY", "TEST_VALUE");
		builder.save();
	}
	
	
	
	
	
	
//	public static void main(String[] args) throws ConfigurationException {
//		
//		AppConfigManager acm = AppConfigManager.getInstance();
//		acm.testSave();
//		
//		
//	}
	
}
